package com.example.model.title.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.DeptModel;
import com.example.auth.model.RoleModel;
import com.example.auth.service.RoleModelService;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.model.TreeModel;
import com.example.model.title.model.ChannelModel;
import com.example.model.title.service.ChannelModelService;
import com.example.utils.cacheutils.CacheUtils;
import com.example.utils.treeutils.TreeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname ChannelModelServiceImpl
 * @Description TODO
 * @Date 2022/8/7 1:05
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class ChannelModelServiceImpl implements ChannelModelService {

    @Autowired
    TableService tableService;

    @Autowired
    RoleModelService roleModelService;

    @Override
    public IPage<ChannelModel> page(Integer pageNumber, Integer pageSize, JSONObject obj) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(ChannelModel.class);
        if(obj != null){

        }
        sqlBuilder.page(pageNumber,pageSize);
        sqlBuilder.orderByDesc("createdate");
        IPage<ChannelModel> channelModelIPage=tableService.getPage(sqlBuilder,ChannelModel.class);
        return channelModelIPage;
    }

    @Override
    public ChannelModel save(ChannelModel channelModel) {
        CacheUtils.remove(ChannelModel.class.getName());
        tableService.save(channelModel,ChannelModel.class);
        return channelModel;
    }

    @Override
    public ChannelModel update(ChannelModel channelModel) {
        CacheUtils.remove(ChannelModel.class.getName());
        tableService.updateById(channelModel,ChannelModel.class);
        return channelModel;
    }

    @Override
    public ChannelModel getId(Object id) {
        ChannelModel channelModel=tableService.getOneById(id,ChannelModel.class);
        if(channelModel!=null && channelModel.getId()!=null){
            RoleModel roleModel=roleModelService.getRoleModelById(String.valueOf(channelModel.getRoid()));
            if(roleModel != null){
                channelModel.setRolename(roleModel.getRolename());
            }
        }
        return channelModel;
    }

    @Override
    public List<TreeModel> treeModelList() {
        List<ChannelModel> channelModels=channelModelList();
        if(channelModels == null || channelModels.size() == 0){
            return Collections.emptyList();
        }
        List<TreeModel> treeModels = new ArrayList<>();
        for (int i = 0; i < channelModels.size() ; i++) {
            ChannelModel channelModel = channelModels.get(i);
            TreeModel treeModel = new TreeModel();
            treeModel.setPid(channelModel.getPid());
            treeModel.setId(channelModel.getId());
            treeModel.setTitle(channelModel.getChannelName());
            treeModels.add(treeModel);
        }
        treeModels= TreeUtils.getTreeModelList(treeModels,0);
        return treeModels;
    }

    @Override
    public List<ChannelModel> channelModelList() {
        Object list=CacheUtils.get(ChannelModel.class.getName());
        if(list==null){
            List<ChannelModel> channelModels=tableService.getList(ChannelModel.class);
            CacheUtils.put(ChannelModel.class.getName(),channelModels,3000);
            return channelModels;
        }
        return (List<ChannelModel>) list;
    }

    @Override
    public List<ChannelModel> channelModelchildList(Object id) {
        List<ChannelModel> channelModels = channelModelList();
        List<ChannelModel> list=getDeptModelChildList(channelModels,id);
        return list;
    }

    private List<ChannelModel> getDeptModelChildList(List<ChannelModel> all, Object id) {
        List<ChannelModel> deptModelList=new ArrayList<>();
        all.stream().filter(e->{
            return String.valueOf(e.getPid()).equals(String.valueOf(id));
        }).map((e)->{
            addchlidList(all,e,deptModelList);
            deptModelList.add(e);
            return e;
        }).collect(Collectors.toList());
        return deptModelList;
    }

    private void addchlidList(List<ChannelModel> all,ChannelModel channelModel,List<ChannelModel> list){
        all.stream().filter(e->{
            return String.valueOf(e.getPid()).equals(String.valueOf(channelModel.getId()));
        }).map((e)->{
            addchlidList(all,e,list);
            list.add(e);
            return e;
        }).collect(Collectors.toList());
    }
}
